package com.megvii.koala.tv.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.megvii.koala.tv.R;

/**
 * Created by linyue on 16/10/12.
 */

public class BoxAddDialog
{
    public BoxAddDialogListener listener;

    private AlertDialog alertDialog;

    synchronized public void show(Context context)
    {
        if (alertDialog != null)
        {
            return;
        }
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setCancelable(true);
        alertDialog.show();
        alertDialog.setCanceledOnTouchOutside(true);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view = mInflater.inflate(R.layout.box_add_dialog, null, false);
        Window window = alertDialog.getWindow();
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setContentView(view);
        Button confirmButton = (Button) view.findViewById(R.id.box_add_confirm);
        Button cancelButton = (Button) view.findViewById(R.id.box_add_cancel);

        confirmButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (listener != null)
                {
                    EditText addressEdit = (EditText) view.findViewById(R.id.box_add_address);
                    listener.onAddBoxAddress(addressEdit.getText().toString());
                }
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (listener != null)
                {
                    listener.onCancelBoxAddDialog();
                }
            }
        });
        alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener()
        {
            @Override
            public void onCancel(DialogInterface dialog)
            {
                if (listener != null)
                {
                    listener.onCancelBoxAddDialog();
                }
            }
        });
    }

    synchronized public void hide()
    {
        if (alertDialog != null)
        {
            alertDialog.cancel();
            alertDialog = null;
        }
    }

    public void setListener(BoxAddDialogListener listener)
    {
        this.listener = listener;
    }

    public static interface BoxAddDialogListener
    {
        public void onAddBoxAddress(String address);

        public void onCancelBoxAddDialog();
    }
}
